<?php

/**
 * 吉信通
 *
 * @author lovebing <lovebing365@126.com> Created on 2015-1-11
 */
class SMSJX extends SMSBase implements SMSInterface {

    protected $apiUrl = 'http://service.winic.org/sys_port/gateway/';
    private static $instances = null;
    
    /**
     * 获取实例id
     * @param string $host
     * @param int $port
     * @param string $user
     * @param string $pwd
     * @return string
     */
    public static function getInstanceId($user = '', $pwd = '') {
        return md5($user . $pwd);
    }

    /**
     * 获取实例
     * @param string $user
     * @param string $pwd
     * @param string $sign
     * @return Email
     */
    public static function getInstance($user = '', $pwd = '', $sign = null) {
        $id = self::getInstanceId($user, $pwd);
        if (!isset(self::$instances[$id]) || !is_a(self::$instances[$id], 'SMS')) {
            self::$instances[$id] = new self($user, $pwd, $sign);
        }
        $sms = self::$instances[$id];
        return $sms;
    }

    private function parseResult($res) {
        // -02/Send:1/Consumption:0/Tmoney:0 / sid
        $arr = explode('/', $res);
        if (empty($arr)) {
            return -100;
        }
        return intval($arr[0]);
    }

    public function send($mobile, $msg) {

        $params = array(
            'id' => urlencode($this->user),
            'pwd' => urlencode($this->pwd),
            'to' => urlencode($mobile), // 手机号 多个用英文的逗号隔开 100个/次
            'content' => iconv('utf-8', 'gb18030', $msg . $this->sign), // 短信内容
            'time' => ''
        );

        $http = HTTPClient::getInstance();
        $res = $http->get($this->apiUrl, $params);
        $errorCode = $http->getErrorCode();
        if (!empty($errorCode)) {
            throw new Exception($http->getErrorMessage(), $errorCode);
        }
        $code = $this->parseResult($res);

        if ($code < 0) {
            throw new Exception($this->getErrorMessage($code), $code);
        }
        return $code;
    }

    public function getErrorMessage($errorCode) {
        $errorCode = intval($errorCode);
        if ($errorCode >= 0) {
            return '';
        }
        $msg = "未知错误{$errorCode}";
        switch ($errorCode) {
            case -1:
                $msg = '当前账号余额不足！';
                break;
            case -2:
                $msg = '当前用户ID错误！';
                break;
            case -3:
                $msg = '当前密码错误！';
                break;
            case -4:
                $msg = '参数不够或参数内容的类型错误！';
                break;
            case -5:
                $msg = '手机号码格式不对！';
                break;
            case -6:
                $msg = '短信内容编码不对！';
                break;
            case -7:
                $msg = '短信内容含有敏感字符！';
                break;
            case -8:
                $msg = '无接收数据';
                break;
            case -9:
                $msg = '系统维护中..';
                break;
            case -10:
                $msg = '手机号码数量超长！（100个/次 超100个请自行做循环）';
                break;
            case -11:
                $msg = '短信内容超长！（70个字符）';
                break;
            case -12:
                $msg = '其它错误！';
                break;
        }
        return $msg;
    }

}
